package com.fuhao.controller;

import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.activemq.broker.scheduler.Job;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.fuhao.base.TextMessage;
import com.fuhao.utils.CheckUtil;
import com.fuhao.utils.HttpClientUtil;
import com.fuhao.utils.XmlUtils;

import ch.qos.logback.core.joran.spi.XMLUtil;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RestController
public class CheckWeixin {
//
//	//微信验证接口地址
//	@RequestMapping(value="/CheckWeixin",method=RequestMethod.GET)
//	public String checkWeixin(String signature,String timestamp,String nonce,String echostr) {
//		//验证参数
//		boolean checkSignature = CheckUtil.checkSignature(signature, timestamp, nonce);
//		//验证成功，返回成功数
//		if(!checkSignature) {
//			return null;
//		}
//		return echostr;
//	}
//	private static final String REQUESRURL="http://api.qingyunke.com/api.php?key=free&appid=0&msg=";
//	//微信动作的推送
//	@RequestMapping(value="/CheckWeixin",method=RequestMethod.POST)
//	public void checkWeixin(HttpServletRequest request,HttpServletResponse response) throws Exception {
//		//解决乱码
//		request.setCharacterEncoding("UTF-8");
//		response.setCharacterEncoding("UTF-8");
//		//将xml转换成map格式
//		Map<String, String> parseXml = XmlUtils.parseXml(request);
//		log.info("####微信接收消息"+parseXml);
//		//判断消息类型,具体消息分类看微信公众平台开发指南的消息管理模块
//		String msgType = parseXml.get("MsgType");
//		PrintWriter writer = response.getWriter();
//		String msg = null;
//
//		//若是文本类型，返回给微信服务端
//		switch(msgType) {
//		case "text":
//			String fromUserName = parseXml.get("FromUserName");
//			String content = parseXml.get("Content");
//			String toUserName = parseXml.get("ToUserName");
//			//接入第三反自动回复机器人接口
//			String resultJson = HttpClientUtil.doGet(REQUESRURL+content);
//			JSONObject jsonObject = JSONObject.parseObject(resultJson);
//			Integer resultCode = jsonObject.getInteger("result");
//			if(resultCode == 0) {
//				String resultContent = (String) jsonObject.get("content");
//				msg = setText(resultContent, toUserName, fromUserName);
//			}else {
//				msg = setText("我现在有点忙，稍后回复您", toUserName, fromUserName);
//			}
////			if(content.equals("百度")) {
////				msg = setText("www.baidu.com", toUserName, fromUserName);
////				
////			}else {
////				msg = setText("未匹配到关键字", toUserName, fromUserName);
////			}
//			writer.println(msg);
//			break;
//		default:
//			break;
//		}
//		writer.close();
//	}
//	public String setText(String content, String fromUserName, String toUserName) {
//		TextMessage textMessage = new TextMessage();
//		textMessage.setContent(content);
//		//文本回复类型
//		textMessage.setMsgType("text");
//		textMessage.setCreateTime(new Date().getTime());
//		textMessage.setFromUserName(fromUserName);
//		textMessage.setToUserName(toUserName);
//		// 将实体类转换成xml格式
//		String msg = XmlUtils.messageToXml(textMessage);
//		return msg;
//	}
	
}
